/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package etf.pia.controlers;

import etf.pia.entity.Obavestenja;
import etf.pia.entity.PlanAngazovanja;
import etf.pia.entity.Predmet;
import etf.pia.entity.Zaposleni;
import etf.pia.util.Initializer;
import etf.pia.util.ServerUtil;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.primefaces.event.ToggleEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author Alejandro
 */
@Getter
@Setter
@ManagedBean(name = "employeeBean")
@SessionScoped
public class EmployeeBean implements Serializable {

    private ArrayList<Zaposleni> employeeList = null;

    public EmployeeBean() {
        load();
    }

    private void load() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Criteria c = ses.createCriteria(Zaposleni.class);
            employeeList = new ArrayList<Zaposleni>(c.list());
            for (Zaposleni z : employeeList) {
                Initializer.initPredmets(z);
            }
            t.commit();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public ArrayList<Predmet> toList(Zaposleni z) {
        if (z == null) {
            return null;
        }
        ArrayList<Predmet> list = new ArrayList<Predmet>();
        for (PlanAngazovanja p : new ArrayList<PlanAngazovanja>(z.getPlanAngazovanjas())) {
            list.add(p.getPredmet());
        }
        return list;
    }

    public StreamedContent getImage(byte[] b) throws IOException {
        if (b == null) {
            return null;
        }
        return new DefaultStreamedContent(new ByteArrayInputStream(b), "image/jpg");
    }

}
